#!/usr/bin/env python3
"""
AI业务处理演示脚本
展示如何使用整合后的Kaggle数据集进行AI业务分析
"""

import pandas as pd
import numpy as np
from pathlib import Path
import json
from datetime import datetime, timedelta
import warnings
warnings.filterwarnings('ignore')

class AIBusinessProcessor:
    def __init__(self):
        self.processed_data_dir = Path("data/processed")
        self.results = {}
        
    def load_datasets(self):
        """加载整合后的数据集"""
        print("=== 加载AI训练数据集 ===")
        
        datasets = {
            "demand_forecast": "ai_demand_forecast_training.csv",
            "inventory_optimization": "ai_inventory_optimization_training.csv", 
            "supplier_analysis": "ai_supplier_analysis_training.csv",
            "anomaly_detection": "ai_anomaly_detection_training.csv",
            "smart_logistics": "kaggle_smart_logistics.csv",
            "scm_enhanced": "kaggle_scm_dataset_updated_with_green_logistics.csv"
        }
        
        loaded_datasets = {}
        
        for name, filename in datasets.items():
            filepath = self.processed_data_dir / filename
            if filepath.exists():
                try:
                    df = pd.read_csv(filepath)
                    loaded_datasets[name] = df
                    print(f"✓ {name}: {len(df)}行 x {len(df.columns)}列")
                except Exception as e:
                    print(f"❌ 加载{name}失败: {e}")
            else:
                print(f"⚠️  文件不存在: {filename}")
        
        return loaded_datasets
    
    def ai_demand_forecasting(self, datasets):
        """AI需求预测分析"""
        print("\n=== AI需求预测分析 ===")
        
        if 'demand_forecast' not in datasets:
            print("❌ 需求预测数据集不可用")
            return
        
        df = datasets['demand_forecast']
        
        try:
            # 基础统计分析
            numeric_cols = df.select_dtypes(include=[np.number]).columns
            
            if len(numeric_cols) > 0:
                # 计算需求趋势
                demand_stats = {
                    "total_records": len(df),
                    "numeric_features": len(numeric_cols),
                    "data_quality": {
                        "missing_values": df.isnull().sum().sum(),
                        "duplicate_records": df.duplicated().sum()
                    }
                }
                
                # 简单的需求预测模拟
                if len(numeric_cols) >= 1:
                    main_col = numeric_cols[0]
                    recent_data = df[main_col].tail(1000)  # 最近1000条记录
                    
                    # 计算移动平均作为预测基线
                    if len(recent_data) >= 30:
                        ma_7 = recent_data.rolling(window=7).mean().iloc[-1]
                        ma_30 = recent_data.rolling(window=30).mean().iloc[-1]
                        
                        demand_stats["forecast_indicators"] = {
                            "7_day_moving_average": round(ma_7, 2) if not pd.isna(ma_7) else 0,
                            "30_day_moving_average": round(ma_30, 2) if not pd.isna(ma_30) else 0,
                            "trend_direction": "上升" if ma_7 > ma_30 else "下降"
                        }
                
                self.results["demand_forecasting"] = demand_stats
                print(f"✓ 需求预测分析完成: {demand_stats['total_records']}条记录")
                print(f"✓ 数据质量: 缺失值{demand_stats['data_quality']['missing_values']}个")
                
                if "forecast_indicators" in demand_stats:
                    indicators = demand_stats["forecast_indicators"]
                    print(f"✓ 预测指标: 7日均值{indicators['7_day_moving_average']}, 趋势{indicators['trend_direction']}")
            
        except Exception as e:
            print(f"❌ 需求预测分析失败: {e}")
    
    def ai_inventory_optimization(self, datasets):
        """AI库存优化分析"""
        print("\n=== AI库存优化分析 ===")
        
        if 'inventory_optimization' not in datasets:
            print("❌ 库存优化数据集不可用")
            return
        
        df = datasets['inventory_optimization']
        
        try:
            # 库存分析
            numeric_cols = df.select_dtypes(include=[np.number]).columns
            
            inventory_stats = {
                "total_items": len(df),
                "features": len(df.columns),
                "analysis_date": datetime.now().isoformat()
            }
            
            if len(numeric_cols) > 0:
                # 库存水平分析
                for col in numeric_cols[:3]:  # 分析前3个数值列
                    col_stats = {
                        "mean": round(df[col].mean(), 2),
                        "std": round(df[col].std(), 2),
                        "min": round(df[col].min(), 2),
                        "max": round(df[col].max(), 2)
                    }
                    inventory_stats[f"{col}_stats"] = col_stats
                
                # 库存优化建议
                main_col = numeric_cols[0]
                q25 = df[main_col].quantile(0.25)
                q75 = df[main_col].quantile(0.75)
                
                low_stock_items = len(df[df[main_col] <= q25])
                high_stock_items = len(df[df[main_col] >= q75])
                
                inventory_stats["optimization_insights"] = {
                    "low_stock_items": low_stock_items,
                    "high_stock_items": high_stock_items,
                    "rebalance_needed": low_stock_items + high_stock_items,
                    "optimization_potential": round((low_stock_items + high_stock_items) / len(df) * 100, 1)
                }
            
            self.results["inventory_optimization"] = inventory_stats
            print(f"✓ 库存优化分析完成: {inventory_stats['total_items']}个项目")
            
            if "optimization_insights" in inventory_stats:
                insights = inventory_stats["optimization_insights"]
                print(f"✓ 优化建议: {insights['rebalance_needed']}个项目需要调整")
                print(f"✓ 优化潜力: {insights['optimization_potential']}%")
            
        except Exception as e:
            print(f"❌ 库存优化分析失败: {e}")
    
    def ai_supplier_analysis(self, datasets):
        """AI供应商分析"""
        print("\n=== AI供应商分析 ===")
        
        if 'supplier_analysis' not in datasets:
            print("❌ 供应商分析数据集不可用")
            return
        
        df = datasets['supplier_analysis']
        
        try:
            supplier_stats = {
                "total_records": len(df),
                "features": len(df.columns),
                "analysis_scope": "供应商绩效评估"
            }
            
            # 供应商绩效分析
            numeric_cols = df.select_dtypes(include=[np.number]).columns
            
            if len(numeric_cols) >= 2:
                # 计算供应商评分（简化版）
                performance_scores = []
                
                for idx, row in df.head(100).iterrows():  # 分析前100个供应商
                    score = 0
                    valid_metrics = 0
                    
                    for col in numeric_cols[:5]:  # 使用前5个指标
                        if not pd.isna(row[col]):
                            # 标准化分数 (0-100)
                            normalized = min(100, max(0, (row[col] - df[col].min()) / (df[col].max() - df[col].min()) * 100))
                            score += normalized
                            valid_metrics += 1
                    
                    if valid_metrics > 0:
                        performance_scores.append(score / valid_metrics)
                
                if performance_scores:
                    avg_performance = round(np.mean(performance_scores), 2)
                    top_performers = len([s for s in performance_scores if s >= 80])
                    poor_performers = len([s for s in performance_scores if s <= 40])
                    
                    supplier_stats["performance_analysis"] = {
                        "average_score": avg_performance,
                        "top_performers": top_performers,
                        "poor_performers": poor_performers,
                        "total_evaluated": len(performance_scores)
                    }
            
            self.results["supplier_analysis"] = supplier_stats
            print(f"✓ 供应商分析完成: {supplier_stats['total_records']}条记录")
            
            if "performance_analysis" in supplier_stats:
                perf = supplier_stats["performance_analysis"]
                print(f"✓ 绩效评估: 平均分{perf['average_score']}, 优秀供应商{perf['top_performers']}个")
            
        except Exception as e:
            print(f"❌ 供应商分析失败: {e}")
    
    def ai_anomaly_detection(self, datasets):
        """AI异常检测分析"""
        print("\n=== AI异常检测分析 ===")
        
        if 'anomaly_detection' not in datasets:
            print("❌ 异常检测数据集不可用")
            return
        
        df = datasets['anomaly_detection']
        
        try:
            anomaly_stats = {
                "total_records": len(df),
                "features": len(df.columns),
                "detection_method": "统计阈值法"
            }
            
            # 异常检测分析
            numeric_cols = df.select_dtypes(include=[np.number]).columns
            anomalies_detected = 0
            
            if len(numeric_cols) > 0:
                # 使用Z-score方法检测异常
                for col in numeric_cols[:5]:  # 检测前5个数值列
                    if col.endswith('_zscore'):  # 使用预计算的Z-score
                        anomalies = df[df[col] > 3]  # Z-score > 3 视为异常
                        anomalies_detected += len(anomalies)
                    else:
                        # 计算Z-score
                        z_scores = np.abs((df[col] - df[col].mean()) / df[col].std())
                        anomalies = df[z_scores > 2.5]  # Z-score > 2.5 视为异常
                        anomalies_detected += len(anomalies)
                
                anomaly_rate = round(anomalies_detected / len(df) / len(numeric_cols[:5]) * 100, 2)
                
                anomaly_stats["detection_results"] = {
                    "anomalies_found": anomalies_detected,
                    "anomaly_rate_percent": anomaly_rate,
                    "normal_records": len(df) - anomalies_detected,
                    "risk_level": "高" if anomaly_rate > 10 else "中" if anomaly_rate > 5 else "低"
                }
            
            self.results["anomaly_detection"] = anomaly_stats
            print(f"✓ 异常检测完成: {anomaly_stats['total_records']}条记录")
            
            if "detection_results" in anomaly_stats:
                results = anomaly_stats["detection_results"]
                print(f"✓ 检测结果: 发现{results['anomalies_found']}个异常, 异常率{results['anomaly_rate_percent']}%")
                print(f"✓ 风险等级: {results['risk_level']}")
            
        except Exception as e:
            print(f"❌ 异常检测分析失败: {e}")
    
    def ai_logistics_optimization(self, datasets):
        """AI物流优化分析"""
        print("\n=== AI物流优化分析 ===")
        
        if 'smart_logistics' not in datasets:
            print("❌ 智能物流数据集不可用")
            return
        
        df = datasets['smart_logistics']
        
        try:
            logistics_stats = {
                "total_shipments": len(df),
                "features": len(df.columns),
                "optimization_focus": "配送效率与成本"
            }
            
            # 物流优化分析
            numeric_cols = df.select_dtypes(include=[np.number]).columns
            
            if len(numeric_cols) >= 2:
                # 配送效率分析
                efficiency_metrics = {}
                
                for col in numeric_cols[:3]:  # 分析前3个指标
                    col_mean = df[col].mean()
                    col_std = df[col].std()
                    
                    efficiency_metrics[col] = {
                        "average": round(col_mean, 2),
                        "variability": round(col_std, 2),
                        "consistency_score": round(100 - (col_std / col_mean * 100), 2) if col_mean != 0 else 0
                    }
                
                # 优化建议
                avg_consistency = np.mean([m["consistency_score"] for m in efficiency_metrics.values()])
                
                logistics_stats["optimization_analysis"] = {
                    "efficiency_metrics": efficiency_metrics,
                    "overall_consistency": round(avg_consistency, 2),
                    "improvement_potential": round(100 - avg_consistency, 2),
                    "optimization_priority": "高" if avg_consistency < 70 else "中" if avg_consistency < 85 else "低"
                }
            
            self.results["logistics_optimization"] = logistics_stats
            print(f"✓ 物流优化分析完成: {logistics_stats['total_shipments']}个配送记录")
            
            if "optimization_analysis" in logistics_stats:
                analysis = logistics_stats["optimization_analysis"]
                print(f"✓ 整体一致性: {analysis['overall_consistency']}%")
                print(f"✓ 改进潜力: {analysis['improvement_potential']}%")
                print(f"✓ 优化优先级: {analysis['optimization_priority']}")
            
        except Exception as e:
            print(f"❌ 物流优化分析失败: {e}")
    
    def generate_business_insights(self):
        """生成业务洞察报告"""
        print("\n=== 生成AI业务洞察报告 ===")
        
        insights = {
            "analysis_date": datetime.now().isoformat(),
            "ai_capabilities": list(self.results.keys()),
            "business_insights": {},
            "recommendations": []
        }
        
        # 综合分析各AI能力的结果
        total_records_analyzed = 0
        high_priority_actions = 0
        
        for capability, results in self.results.items():
            if "total_records" in results:
                total_records_analyzed += results["total_records"]
            elif "total_items" in results:
                total_records_analyzed += results["total_items"]
            elif "total_shipments" in results:
                total_records_analyzed += results["total_shipments"]
            
            # 提取关键洞察
            if capability == "demand_forecasting" and "forecast_indicators" in results:
                trend = results["forecast_indicators"]["trend_direction"]
                insights["business_insights"]["demand_trend"] = trend
                if trend == "上升":
                    insights["recommendations"].append("需求呈上升趋势，建议增加库存准备")
                else:
                    insights["recommendations"].append("需求呈下降趋势，建议优化库存结构")
            
            if capability == "inventory_optimization" and "optimization_insights" in results:
                potential = results["optimization_insights"]["optimization_potential"]
                if potential > 20:
                    high_priority_actions += 1
                    insights["recommendations"].append(f"库存优化潜力{potential}%，建议立即调整库存结构")
            
            if capability == "supplier_analysis" and "performance_analysis" in results:
                poor_performers = results["performance_analysis"]["poor_performers"]
                if poor_performers > 0:
                    high_priority_actions += 1
                    insights["recommendations"].append(f"发现{poor_performers}个低绩效供应商，建议评估替换方案")
            
            if capability == "anomaly_detection" and "detection_results" in results:
                risk_level = results["detection_results"]["risk_level"]
                if risk_level in ["高", "中"]:
                    high_priority_actions += 1
                    insights["recommendations"].append(f"检测到{risk_level}风险异常，建议加强监控")
            
            if capability == "logistics_optimization" and "optimization_analysis" in results:
                priority = results["optimization_analysis"]["optimization_priority"]
                if priority == "高":
                    high_priority_actions += 1
                    insights["recommendations"].append("物流效率需要优化，建议重新规划配送路线")
        
        insights["summary"] = {
            "total_records_analyzed": total_records_analyzed,
            "ai_capabilities_used": len(self.results),
            "high_priority_actions": high_priority_actions,
            "business_readiness": "就绪" if len(self.results) >= 4 else "部分就绪"
        }
        
        # 保存洞察报告
        report_file = Path("AI_Business_Insights_Report.json")
        
        # 转换numpy类型为Python原生类型
        def convert_numpy_types(obj):
            if isinstance(obj, dict):
                return {key: convert_numpy_types(value) for key, value in obj.items()}
            elif isinstance(obj, list):
                return [convert_numpy_types(item) for item in obj]
            elif isinstance(obj, np.integer):
                return int(obj)
            elif isinstance(obj, np.floating):
                return float(obj)
            elif isinstance(obj, np.ndarray):
                return obj.tolist()
            else:
                return obj
        
        report_data = {
            "insights": convert_numpy_types(insights),
            "detailed_results": convert_numpy_types(self.results)
        }
        
        with open(report_file, 'w', encoding='utf-8') as f:
            json.dump(report_data, f, indent=2, ensure_ascii=False)
        
        print(f"✓ 业务洞察报告已保存: {report_file}")
        print(f"✓ 分析记录总数: {insights['summary']['total_records_analyzed']:,}条")
        print(f"✓ AI能力使用: {insights['summary']['ai_capabilities_used']}个")
        print(f"✓ 高优先级行动: {insights['summary']['high_priority_actions']}项")
        print(f"✓ 业务就绪状态: {insights['summary']['business_readiness']}")
        
        return insights
    
    def run_ai_business_processing(self):
        """运行完整的AI业务处理流程"""
        print("=== AI业务处理演示开始 ===")
        print()
        
        # 加载数据集
        datasets = self.load_datasets()
        
        if not datasets:
            print("❌ 没有可用的数据集")
            return
        
        # 运行各种AI分析
        self.ai_demand_forecasting(datasets)
        self.ai_inventory_optimization(datasets)
        self.ai_supplier_analysis(datasets)
        self.ai_anomaly_detection(datasets)
        self.ai_logistics_optimization(datasets)
        
        # 生成业务洞察
        insights = self.generate_business_insights()
        
        print(f"\n=== AI业务处理完成 ===")
        print(f"✓ 成功运行{len(self.results)}个AI分析模块")
        print(f"✓ 生成{len(insights['recommendations'])}条业务建议")
        print(f"✓ AI系统已准备就绪，可进行实时业务处理")
        
        return insights

if __name__ == "__main__":
    processor = AIBusinessProcessor()
    processor.run_ai_business_processing()